package com.ErpCoreWeb.Security.User;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ErpCoreModel.Base.CCompany;
import com.ErpCoreModel.Base.CUser;
import com.ErpCoreModel.Framework.CBaseObject;
import com.ErpCoreModel.Framework.CColumn;
import com.ErpCoreModel.Framework.CTable;
import com.ErpCoreModel.Framework.Util;
import com.ErpCoreWeb.Common.Global;

/**
 * Servlet implementation class AddUser
 */
@WebServlet("/AddUser")
public class AddUser extends HttpServlet {
	private static final long serialVersionUID = 1L;

	HttpServletRequest request;
	HttpServletResponse response;

	public CUser m_User = null;
    public CTable m_Table = null;
    public CCompany m_Company = null;  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddUser() {
        super();
        // TODO Auto-generated constructor stub
    }

    //初始化变量
    void initData()
    {
        if (request.getSession().getAttribute("User") == null)
        {
            try {
				response.sendRedirect("../Login.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            return ;
        }
        m_User=(CUser)request.getSession().getAttribute("User");
        if (!m_User.IsRole("管理员"))
        {
        	try {
				response.getWriter().print("没有管理员权限！");
	        	response.getWriter().close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	return ;
        }
        String B_Company_id = request.getParameter("B_Company_id");
		if (Global.IsNullParameter(B_Company_id))
			m_Company = Global.GetCtx(this.getServletContext())
					.getCompanyMgr().FindTopCompany();
		else
			m_Company = (CCompany) Global.GetCtx(this.getServletContext())
					.getCompanyMgr().Find(Util.GetUUID(B_Company_id));

        m_Table = (CTable)Global.GetCtx(this.getServletContext()).getUserMgr().getTable();
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProc(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProc(request,response);
	}
	void doProc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{

		this.request=request;
		this.response=response;

		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");

		//初始化变量
	    initData();
	    //
        String Action = request.getParameter("Action");
        if (Action == null) Action = "";

        if (Action.equalsIgnoreCase("Cancel"))
        {
            return ;
        }
        else if (Action.equalsIgnoreCase("PostData"))
        {
        	PostData();
            return ;
        }
	}
    void PostData()
    {
        CUser user = (CUser)request.getSession().getAttribute("User");
        CUser BaseObject = new CUser();
        BaseObject.Ctx = Global.GetCtx(this.getServletContext());
        BaseObject.setCreator( user.getId());

        List<CBaseObject> lstCol = m_Table.getColumnMgr().GetList();
        boolean bHasVisible = false;
        for (CBaseObject obj : lstCol)
        {
            CColumn col = (CColumn)obj;

            if (col.getCode().equalsIgnoreCase("id"))
                continue;
            else if (col.getCode().equalsIgnoreCase("Created"))
                continue;
            else if (col.getCode().equalsIgnoreCase("Creator"))
            {
                //BaseObject.SetColValue(col, Program.User.Id);
                continue;
            }
            else if (col.getCode().equalsIgnoreCase("Updated"))
                continue;
            else if (col.getCode().equalsIgnoreCase("Updator"))
            {
                //BaseObject.SetColValue(col, Program.User.Id);
                continue;
            }

            BaseObject.SetColValue(col, request.getParameter(col.getCode()));
            bHasVisible = true;
        }
        if (!bHasVisible)
        {
            try {
				response.getWriter().print("没有可修改字段！");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            return ;
        }
        if (!Global.GetCtx(this.getServletContext()).getUserMgr().AddNew(BaseObject, true))
        {
        	try {
				response.getWriter().print("添加失败！");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            return ;
        }
    }
}
